package com.winway.gggapp.ui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


import com.winway.gggapp.R;
import com.winway.gggapp.bean.Category;
import com.winway.gggapp.common.CustomSimpleAdapter;
import com.winway.gggapp.common.StringUtils;
import com.winway.gggapp.util.DensityUtil;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Record extends Activity {
	
	private static final String TAG="Record";
	private final int COLUMNWIDTHPX = 55;
	private final int FLINGVELOCITYPX = 800; // 滚动距离
	private int mCid;
	private String mCatName;
	
	private int mColumnWidthDip;
	private int mFlingVelocityDip;
	private TextView recordform_title1;
	private Button recordform_btn_save;
	private Button recordform_btn_saveandshare;
	private EditText recordform_edit_type;
	private EditText recordform_zm;
	private EditText recordform_fm;
	private EditText recordform_wy;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.record);
		
		//默认选中的功过格分类
		mCid = 1;
		mCatName ="国内";
		
		recordform_title1 = (TextView)findViewById(R.id.recordform_title1);
		recordform_btn_save = (Button)findViewById(R.id.recordform_btn_save);
		recordform_btn_saveandshare = (Button)findViewById(R.id.recordform_btn_saveandshare);
		recordform_edit_type = (EditText)findViewById(R.id.recordform_type);
		recordform_zm = (EditText)findViewById(R.id.recordform_zm);
		recordform_fm = (EditText)findViewById(R.id.recordform_fm);
		recordform_wy = (EditText)findViewById(R.id.recordform_wy);
		
		recordform_btn_save.setOnClickListener(mListener);
		recordform_btn_saveandshare.setOnClickListener(mListener);
		recordform_edit_type.setText(""+mCid);
		
		//把px转换成dip
		mColumnWidthDip = DensityUtil.px2dip(this, COLUMNWIDTHPX);
		mFlingVelocityDip = DensityUtil.px2dip(this, FLINGVELOCITYPX);
		
		//获取功过格分类
		String[] categoryArray = getResources().getStringArray(R.array.gggtypes);
		//把功过格分类保存到List中
		final List<HashMap<String, Category>> categories = new ArrayList<HashMap<String, Category>>();
		//分割功过格类型字符串
		for(int i=0;i<categoryArray.length;i++)
		{
			String[] temp = categoryArray[i].split("[|]");
			if (temp.length==2)
			{
				int cid = StringUtils.String2Int(temp[0]);
				String title = temp[1];
				Category type = new Category(cid, title);
				HashMap<String, Category> hashMap = new HashMap<String, Category>();
				hashMap.put("category_title", type);
				categories.add(hashMap);
			}
		}
		
		//创建Adapter，指明映射字段
		CustomSimpleAdapter categoryAdapter = new CustomSimpleAdapter(this, categories, R.layout.category_title, new String[]{"category_title"}, new int[]{R.id.category_title});
		
		GridView category = new GridView(this);
		category.setColumnWidth(mColumnWidthDip);//每个单元格宽度
		category.setNumColumns(GridView.AUTO_FIT);//单元格数目
		category.setGravity(Gravity.CENTER);//设置对其方式
		//设置单元格选择是背景色为透明，这样选择时就不现实黄色北京
		category.setSelector(new ColorDrawable(Color.TRANSPARENT));
		//根据单元格宽度和数目计算总宽度
		int width = mColumnWidthDip * categories.size();
		LayoutParams params = new LayoutParams(width, LayoutParams.FILL_PARENT);
		//更新category宽度和高度，这样category在一行显示
		category.setLayoutParams(params);
		//设置适配器
		category.setAdapter(categoryAdapter);
		//把category加入到容器中
		LinearLayout categoryList = (LinearLayout) findViewById(R.id.category_layout);
		categoryList.addView(category);
		//添加单元格点击事件
		category.setOnItemClickListener(new OnItemClickListener()
		{
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id)
			{
				TextView categoryTitle;
				//恢复每个单元格背景色
				for (int i = 0; i < parent.getChildCount(); i++)
				{
					categoryTitle = (TextView) (parent.getChildAt(i));
					categoryTitle.setBackgroundDrawable(null);
					categoryTitle.setTextColor(0XFFADB2AD);
				}
				//设置选择单元格的背景色
				categoryTitle = (TextView) (parent.getChildAt(position));
				categoryTitle.setBackgroundResource(R.drawable.categorybar_item_background);
				categoryTitle.setTextColor(0XFFFFFFFF);
				//获取选中的功过格分类id
				mCid = categories.get(position).get("category_title").getCid();
				mCatName = categories.get(position).get("category_title").getTitle();
				
				String titleString ="您选择的是"+mCatName+"种子";
				recordform_title1.setText(titleString);
				recordform_edit_type.setText(""+mCid);
			}
		});
		// 箭头
		final HorizontalScrollView categoryScrollview = (HorizontalScrollView) findViewById(R.id.category_scrollview);
		Button categoryArrowRight = (Button) findViewById(R.id.category_arrow_right);
		categoryArrowRight.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				categoryScrollview.fling(DensityUtil.px2dip(Record.this, mFlingVelocityDip));
			}
		});
	}
	//实现OnClickListener接口
	OnClickListener mListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.recordform_btn_save:
				savedata();
				break;
			case R.id.recordform_btn_saveandshare:
				saveandsharedata();
				break;
			}
		}
	};
	
	/**
	 * 
	 * 保存功过格
	 * 
	 */
	public void savedata(){
		Log.v(TAG, "保存数据");
		String recordform_edit_type_str = recordform_edit_type.getText().toString().trim();
		String recordform_zm_str = recordform_zm.getText().toString().trim();
		String recordform_fm_str = recordform_fm.getText().toString().trim();
		String recordform_wy_str = recordform_wy.getText().toString().trim();
		
		Log.v(TAG, recordform_edit_type_str);
		Log.v(TAG, recordform_zm_str);
		Log.v(TAG, recordform_fm_str);
		Log.v(TAG, recordform_wy_str);
	}
	/**
	 * 
	 * 保存并共享功过格
	 * 
	 */
	public void saveandsharedata(){
		Log.v(TAG, "保存并共享数据");
		String recordform_edit_type_str = recordform_edit_type.getText().toString().trim();
		String recordform_zm_str = recordform_zm.getText().toString().trim();
		String recordform_fm_str = recordform_fm.getText().toString().trim();
		String recordform_wy_str = recordform_wy.getText().toString().trim();
		Log.v(TAG, recordform_edit_type_str);
		Log.v(TAG, recordform_zm_str);
		Log.v(TAG, recordform_fm_str);
		Log.v(TAG, recordform_wy_str);
	}

}
